<template>
  <BasicModal @register="register" v-bind="$attrs" title="付款信息" @ok="handleOk">
    <BasicForm v-if="show" @register="registerForm" />
    <div v-if="!show" style="font-weight: bold"
      >尚无报备代付信息，无权操作，您是否要添加报备信息？</div
    >
  </BasicModal>
</template>

<script setup lang="ts">
  import { ref, onMounted } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form';
  import { GetReportIndexApi, GrabOrderApi, ReportingApi } from '@/api/payment';
  import { useRouter } from 'vue-router';
  import { message } from 'ant-design-vue';

  onMounted(async () => {
    const data = await GetReportIndexApi({ reviewStatus: '1', page: 1, pageSize: 1000 });
    if (!data || data.list.length < 1) {
      show.value = false;
    }
  });

  const router = useRouter();
  const show = ref(true);
  const info = ref({});
  const formRef = ref({
    review: null,
  });
  const emit = defineEmits(['ok']);
  const [registerForm] = useForm({
    labelWidth: 100,
    schemas: [
      {
        field: 'review',
        label: '付款信息',
        component: 'ApiSelect',
        colProps: {
          span: 24,
        },
        componentProps: {
          api: GetReportIndexApi,
          params: { reviewStatus: '1', page: 1, pageSize: 1000 },
          placeholder: '请选择报备的付款信息',
          resultField: 'list',
          labelField: 'name',
          valueField: 'id',
          onChange: (value) => {
            formRef.value.review = value;
          },
        },
      },
    ],
    showActionButtonGroup: false,
  });
  const report = ref(false);

  let reloadFunc = () => {};

  const [register, { closeModal }] = useModalInner((data) => {
    info.value = data.info;
    report.value = data.report;
    reloadFunc = data.reload;
  });

  const handleOk = () => {
    if (!show.value) {
      router.push({ name: 'PaymentAdd' });
    } else {
      if (report.value) {
        ReportingApi({ orderNo: info.value.orderNo, review: formRef.value.review }).then(() => {
          message.success('重置成功');
          reloadFunc();
          closeModal();
          emit('ok');
        });
        return;
      }
      GrabOrderApi({ orderNo: info.value.orderNo, review: formRef.value.review }).then(() => {
        message.success('抢单成功');
        reloadFunc();
        closeModal();
        emit('ok');
      });
    }
  };
</script>
